package com.guodu.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.PurchaseDistributionPlantzDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.FileTechnicaltz;
import com.guodu.entity.PurchaseDistributionPlantz;
import com.guodu.entity.PurchasedistributiontzStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.PurchaseDistributionPlantzService;
import com.guodu.vo.PurchaseDistributionPlantzQueryVo;
import com.guodu.vo.PurchaseDistributionPlantzVo;

@Service("purchaseDistributionPlantzService")
public class PurchaseDistributionPlantzServiceImpl implements PurchaseDistributionPlantzService {

	
	@Resource
	private PurchaseDistributionPlantzDao purchaseDistributionPlantzDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	
	@Override
	public long findSizeByVo(PurchaseDistributionPlantzQueryVo pdpq, Units units) {
		// TODO Auto-generated method stub
		return purchaseDistributionPlantzDao.findSizeByVo(pdpq, units);
	}

	@Override
	public List<PurchaseDistributionPlantzVo> findByVo(
			PurchaseDistributionPlantzQueryVo pdpq, Units units, int curPage, int pageSize)  {
		// TODO Auto-generated method stub
		List<PurchaseDistributionPlantz> list = purchaseDistributionPlantzDao.findByVo(pdpq, units, curPage, pageSize);
		List<PurchaseDistributionPlantzVo> purchaseDistributionPlantzVos = new ArrayList<PurchaseDistributionPlantzVo>();
		for(PurchaseDistributionPlantz purchaseDistributionPlantz : list ){
			PurchaseDistributionPlantzVo purchaseDistributionPlantzVo = new PurchaseDistributionPlantzVo(purchaseDistributionPlantz);
			UserInfo userInfo = userInfoDao.findById(purchaseDistributionPlantz.getCreatePeople());
			Units unitsInfo = unitsDao.findById(userInfo.getUnitsId());
			List<FileTechnicaltz> fileTechnicaltz = purchaseDistributionPlantzDao.findFile(purchaseDistributionPlantz.getTechnical());
			purchaseDistributionPlantzVo.setUnitsName(unitsInfo.getUnitsName());
			purchaseDistributionPlantzVo.setIndustry(unitsInfo.getIndustry());
			for (FileTechnicaltz fileTechnicaltz2 : fileTechnicaltz) {
				purchaseDistributionPlantzVo.setFileTechnicalName(fileTechnicaltz2.getFileTechnicalName());
				purchaseDistributionPlantzVo.setFileName(fileTechnicaltz2.getFileName());
				purchaseDistributionPlantzVo.setFileUrl(fileTechnicaltz2.getFileRealname());
			}
			if(unitsInfo.getFunitsId()!=null){
				Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
				purchaseDistributionPlantzVo.setFunitsName(funitsInfo.getUnitsName());
			}
			purchaseDistributionPlantzVo.setUserName(userInfo.getUserName());
			purchaseDistributionPlantzVos.add(purchaseDistributionPlantzVo);
		}
		return purchaseDistributionPlantzVos;
	}

	@Override
	public void save(PurchaseDistributionPlantz purchaseDistributionPlan) {
		// TODO Auto-generated method stub
		purchaseDistributionPlantzDao.save(purchaseDistributionPlan);
		
	}

	@Override
	public PurchaseDistributionPlantz findById(String purchaseId) {
		// TODO Auto-generated method stub
		return purchaseDistributionPlantzDao.findById(purchaseId);
	}

	@Override
	public void update(PurchaseDistributionPlantz purchaseDistributionPlantz,Long status) {
		// TODO Auto-generated method stub
		purchaseDistributionPlantzDao.update(purchaseDistributionPlantz);
		List<PurchasedistributiontzStatus> entities = new ArrayList<PurchasedistributiontzStatus>();
		PurchasedistributiontzStatus entity = new PurchasedistributiontzStatus();
		entity.setAfterStatus(Long.valueOf(0));
		entity.setBeforeStatus(status);
		entity.setCreatePeople(purchaseDistributionPlantz.getCreatePeople());
		entity.setPurchaseId(purchaseDistributionPlantz.getPurchaseId());
		entity.setStatusId("1");
		entity.setCreateTime(Calendar.getInstance().getTime());
		entities.add(entity);
		purchaseDistributionPlantzDao.changeStatus(entities);
	}

	@Override
	public List<PurchasedistributiontzStatus> findStatus(String purchaseId) {
		// TODO Auto-generated method stub
		return purchaseDistributionPlantzDao.findStatus(purchaseId);
	}

	@Override
	public void examine(String ids, String rs, String beforeStatus,
			Long status, UserInfo user) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<PurchasedistributiontzStatus> entities = new ArrayList<PurchasedistributiontzStatus>();
		for(int i=0;i<idses.length;i++){
			PurchasedistributiontzStatus entity = new PurchasedistributiontzStatus();
			entity.setAfterStatus(status);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(user.getUserId());
			entity.setPurchaseId(idses[i]);
			entity.setReason(rs);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		purchaseDistributionPlantzDao.changeStatus(entities);
		purchaseDistributionPlantzDao.examine(ids,status);
	}

	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		for (int i = 0; i < delIds.length; i++) {
			PurchaseDistributionPlantz purchaseDistributionPlantz = purchaseDistributionPlantzDao.findById(delIds[i]);
			purchaseDistributionPlantzDao.delZi(purchaseDistributionPlantz.getTechnical());
		}
		purchaseDistributionPlantzDao.delByIds(delIds);
	}

	@Override
	public void saveFile(FileTechnicaltz fileTechnicaltz) {
		// TODO Auto-generated method stub
		purchaseDistributionPlantzDao.save(fileTechnicaltz);
	}

	@Override
	public List<FileTechnicaltz> findFile(String technical) {
		// TODO Auto-generated method stub
		return purchaseDistributionPlantzDao.findFile(technical);
	}

	@Override
	public void delByOne(String technical) {
		// TODO Auto-generated method stub
		purchaseDistributionPlantzDao.delZi(technical);
	}

	@Override
	public List<PurchaseDistributionPlantz> findAllByVo(
			PurchaseDistributionPlantzQueryVo pdpq, Units attribute) {
		// TODO Auto-generated method stub
		return purchaseDistributionPlantzDao.findAllByVo(pdpq,attribute);
	}

	@Override
	public PurchaseDistributionPlantzVo findAllMoney(
			PurchaseDistributionPlantzQueryVo pdpq, Units units) {
		// TODO Auto-generated method stub
		return purchaseDistributionPlantzDao.findAllMoney(pdpq, units);
	}

	@Override
	public void examinAll(PurchaseDistributionPlantzQueryVo pdpq, Units units) {
		// TODO Auto-generated method stub
		purchaseDistributionPlantzDao.examinAll(pdpq,units);
	}

}
